package com.twu.biblioteca;

import java.util.Scanner;
   import java.io.*;
/**
 * Created with IntelliJ IDEA.
 * User: PREETHI
 * Date: 27/5/13
 * Time: 1:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserAccountManager
{         Scanner scan =new Scanner(System.in);
          private user loggedInUser;

          userList userlist;
    public UserAccountManager(user loggedInUser) {
        this.setLoggedInUser(loggedInUser);
         userlist=new userList();

    }

    public user respond(int index) {
        String username,password;

        if(index==5)
        {
            if(getLoggedInUser() !=null)
            {
                System.out.println(MESSAGES.ALREADY_LOGGED_IN);
            }
            else
            {
            System.out.println("Enter username:");
              username=scan.next();
            System.out.println("Enter password:");

            password=scan.next();
             login(username,password);
            }

        }
        else if(index==6)
              logout();
        else if(index==7)
            viewMembership();

         return getLoggedInUser();

    }

    private void viewMembership() {
          if(getLoggedInUser() ==null)
          {
              System.out.println(MESSAGES.MEMBERSHIP_DETAILS_LOGGEDOUT);
          }
          else
          {
              System.out.println("Name:"+ getLoggedInUser().getUsername());
              System.out.println("Email:"+ getLoggedInUser().getEmail());
              System.out.println("Phone No.:"+ getLoggedInUser().getPhoneno());
          }

    }

    private void logout() {

        if(getLoggedInUser() ==null)
        {
            System.out.println(MESSAGES.FIRST_LOGIN_TO_LOGOUT);
        }
        else
        {
            System.out.println(MESSAGES.LOGGED_OUT_SUCCESSFUL);
            this.setLoggedInUser(null);
        }
    }

    private void login(String username, String password) {

        if(userlist.getLoginInfo().containsKey(username))
        {
            user u=userlist.getLoginInfo().get(username);
            if(u.getPwd().equals(password))
            {
                setLoggedInUser(u);
                System.out.println(MESSAGES.LOGGED_IN_SUCCESSFUL);
            }
            else
            {
                System.out.println(MESSAGES.INVALID_PASSWORD);

            }
        }
        else
            System.out.println(MESSAGES.INVALID_USER);


    }

    public user getLoggedInUser() {
        return loggedInUser;
    }

    public void setLoggedInUser(user loggedInUser) {
        this.loggedInUser = loggedInUser;
    }
}
